package com.itc.bi.utils.FilterComponent;

import com.alibaba.fastjson2.JSONArray;
import com.itc.bi.constant.SystemConstant;
import com.itc.common.core.exception.CheckedException;
import com.itc.common.core.exception.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

@Data
public class FieldColumn {
    @ApiModelProperty(value = "字段中文名称")
    String label;

    @ApiModelProperty(value = "字段名称")
    String fieldName;

    @ApiModelProperty(value = "条件")
    String filterCondition;

    @ApiModelProperty(value = "值")
    Object value1;

    @ApiModelProperty(value = "值")
    Object value2;

    public FieldColumn(){}
    public FieldColumn(String label,String fieldName, String filterCondition, String value1){
        this.label = label;
        this.fieldName = fieldName;
        this.filterCondition = filterCondition;
        this.value1 = value1;
    }
    public FieldColumn(String label,String fieldName, String filterCondition, String value1, String value2){
        this.label = label;
        this.fieldName = fieldName;
        this.filterCondition = filterCondition;
        this.value1 = value1;
        this.value2 = value2;
    }

    public String getExp(){
        if(StringUtils.isBlank(filterCondition)){
            throw new CheckedException(String.format("参数 %s 对应条件为空",label));
        }
        //非空时校验值是否为空
        if(!StringUtils.equalsAny(filterCondition,SystemConstant.FILTER_CONDITION_NULL,SystemConstant.FILTER_CONDITION_NONULL)){
            if(ObjectUtils.isEmpty(value1) || "".equals(value1)){
                throw new CheckedException(String.format("参数 %s 对应值为空",label));
            }
            if(SystemConstant.FILTER_CONDITION_BA.equals(filterCondition)){
                if(ObjectUtils.isEmpty(value2) || "".equals(value2)){
                    throw new CheckedException(String.format("参数 %s 对应第二个值为空",label));
                }
            }
        }
        String fieldNameStr = "`"+fieldName+"`";

        switch (filterCondition){
            case SystemConstant.FILTER_CONDITION_EQ:
                return fieldNameStr+" = '"+value1+"'";
            case SystemConstant.FILTER_CONDITION_NE:
                return fieldNameStr+" != '"+value1+"'";
            case SystemConstant.FILTER_CONDITION_IN:
                ArrayList values =(ArrayList)value1;
                StringBuffer inBuffer = new StringBuffer(fieldNameStr).append(" in (");
                for (int i = 0; i < values.size(); i++) {
                    if(i > 0){
                        inBuffer.append(",");
                    }
                    inBuffer.append("'").append(values.get(i)).append("'");
                }
                inBuffer.append(" )");
                return inBuffer.toString();
            case SystemConstant.FILTER_CONDITION_NOIN:
                JSONArray notValues =(JSONArray)value1;
                StringBuffer notInBuffer = new StringBuffer(fieldNameStr).append(" not in (");
                for (int i = 0; i < notValues.size(); i++) {
                    if(i > 0){
                        notInBuffer.append(",");
                    }
                    notInBuffer.append("'").append(notValues.get(i)).append("'");
                }
                notInBuffer.append(" )");
                return notInBuffer.toString();
            case SystemConstant.FILTER_CONDITION_LIKE:
                return fieldNameStr+" like '%"+value1+"%'";
            case SystemConstant.FILTER_CONDITION_NOLIKE:
                return fieldNameStr+" not like '%"+value1+"%'";
            case SystemConstant.FILTER_CONDITION_NULL:
                return fieldNameStr+" is null";
            case SystemConstant.FILTER_CONDITION_NONULL:
                return fieldNameStr+" is not null";
            case SystemConstant.FILTER_CONDITION_GT:
                return fieldNameStr+" > '"+value1+"'";
            case SystemConstant.FILTER_CONDITION_GE:
                return fieldNameStr+" >= '"+value1+"'";
            case SystemConstant.FILTER_CONDITION_LT:
                return fieldNameStr+" < '"+value1+"'";
            case SystemConstant.FILTER_CONDITION_LE:
                return fieldNameStr+" <= '"+value1+"'";
            case SystemConstant.FILTER_CONDITION_BA:
                return "( "+fieldNameStr+" >= '"+value1+"' and "+fieldNameStr+" <= '"+value2+"' )";
            default:throw new ServiceException(String.format("暂不支持 %s 的表达式",filterCondition));
        }
    }

}
